package jvs.tea.html;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import jvs.OS;
import x.java.io.File;
import x.java.tea.FilterInputStream;
import x.java.tea.MapInjector;

public class FileObjectInputStream extends FilterInputStream {
	//private static final String[] WATCH_PATH = new String[] { "/my/watch" };

	//private static final String[] SHARE_PATH = new String[] { "/public",
	//		"/shared", "/my/public", "/my/shared" };

	protected static final int MAX_BCNAME_LEN = 8;

	protected static final int MAX_NAME_LEN = 16;

	private static final String FOOTER = jvs.OS.copyright;

	private final SimpleDateFormat df = new SimpleDateFormat(
			Constant.DATE_FORMAT);

	// private final SimpleDateFormat nowdf = new SimpleDateFormat(
	// Constant.FULL_DATE_FORMAT);

	protected MapInjector map;

	protected File.Info file;
	protected URI context;
	protected URI asset;
	protected String style;
	protected String basehref;
	protected String iconpath;
	protected String root; // context
	protected String assethref;

	public FileObjectInputStream(URI context, URI asset, File.Info file) {
		this.context = (context == null) ? Constant.ROOT_URI : context;
		this.asset = asset;
		this.file = file;
		this.assethref = asset.toString();
		//
		style = assethref + Constant.STYLE;
		iconpath = assethref + Constant.ICON_PATH;
		basehref = context.toString();
		root = basehref;
		//
		init();
	}

	protected void addInjector(String key, Object obj) {
		map.put(key, obj);
	}

	private void init() {
		map = new MapInjector();
		map.put("title", makeTitle(file.getPath()));
		map.put("basehref", basehref);
		map.put("style", style);
		map.put("iconpath", iconpath);
		map.put("assethref", assethref);
		// String now = nowdf.format(new Date());
		// map.put("now", now);
		map.put("appname", jvs.OS.name);
		map.put("applink", jvs.OS.applink);
		// map.put("user", "");
		// map.put("jid", "");
		map.put("footer", FOOTER);
		//
		map.put("root", context);
		map.put("filepath", file.getPath());
		map.put("filename", file.getName());
		map.put("filesize", File.formatSize(file.length));
		map.put("filedate", formatDate(new Date(file.lastModified)));

		setInjector(map);
	}

	private CharSequence makeTitle(String s) {
		if (s == null) {
			return OS.name;
		}
		return OS.name + " - " + s;
	}

	protected String getExt(String name) {
		int idx = name.lastIndexOf(".");
		if (idx > 0) {
			return name.substring(idx + 1);
		}
		return null;
	}

	protected static String makeBreadcrumbs(String root, String p, boolean isFolder) {
		int prefix = p.startsWith("v:/") ? 3 : 1;
		String[] sa = p.substring(prefix).split("/");

		StringBuilder bc = new StringBuilder();
		bc.append("<a href=\"");
		bc.append(root); // "v:///"
		bc.append("\">");
		bc.append("v:/"); // display name
		bc.append("</a>&nbsp;&raquo;&nbsp;");
		//
		if (p.length() <= prefix) {
			return bc.toString();
		}
		StringBuilder link = new StringBuilder(root);
		link.append(root.endsWith("/") ? "" : "/");
		int len = (isFolder ? sa.length : sa.length - 1);
		for (int i = 0; i < len; i++) {
			String n = sa[i];
			link.append(n);
			link.append("/");
			//
			bc.append("<a href=\"");
			bc.append(link.toString());
			bc.append("\">");
			bc.append(shorten(n, MAX_BCNAME_LEN));
			bc.append("</a>&nbsp;&raquo;&nbsp;");
		}
		String s = bc.toString();
		//
		return s;
	}

	protected String formatDate(Date date) {
		if (date.getTime() <= 0) {
			return "-";
		}
		return df.format(date);
	}

	protected void formatDate(Calendar cal, StringBuffer sb) {
		// "yyyy-MM-dd HH:mm"
		sb.append(cal.get(Calendar.YEAR));
		sb.append("-");
		sb.append(cal.get(Calendar.MONTH) + 1);
		sb.append("-");
		sb.append(cal.get(Calendar.DAY_OF_MONTH));
		sb.append(" ");
		sb.append(cal.get(Calendar.HOUR_OF_DAY));
		sb.append(":");
		sb.append(cal.get(Calendar.MINUTE));
	}

	protected static String shorten(String s, int max) {
		if (s.length() > max) {
			return s.substring(0, max) + "...";
		}
		return s;
	}

	protected void addMenu(StringBuilder sb, String link, String name) {
		sb.append("<a href=\"");
		sb.append(link);
		sb.append("\" target=\"_self\"");
		sb.append(">");
		sb.append(name);
		sb.append("</a>");
	}

	protected void addAction(StringBuilder sb, String link, String title,
			String icon) {
		sb.append("<a href=\"");
		sb.append(link);
		sb.append("\" target=\"_self\" >");
		sb.append("<img src=\"");
		sb.append(icon);
		sb.append("\" />");
		sb.append(title);
		sb.append("</a>");
	}

//	private boolean isPath(String path, String s) {
//		return (path.equals(s) || path.startsWith(s + "/"));
//	}
//	
//	private boolean isPath(String path, String[] sa) {
//		for (int i = 0; i < sa.length; i++) {
//			if (isPath(path, sa[i])) {
//				return true;
//			}
//		}
//		return false;
//	}

	protected void menu(StringBuilder sb, String[][] items, String path) {
		for (int i = 0; i < items.length; i++) {
			String[] ai = items[i];
			String cmd = ai[0] + "!" + path;
			addMenu(sb, cmd, ai[2]);
		}
	}

	protected void smartaction(StringBuilder sb, String[][] items, String path) {
		for (int i = 0; i < items.length; i++) {
			String[] ai = items[i];
			String cmd = "v-bin-shell:///bin/" + ai[0] + "!" + path;
			addAction(sb, cmd, "", iconpath + ai[1]);
		}
	}

}